﻿(function ($) {
    $.extend({
        formatUrl: function (url) {
            if (!url) return url;
            if (url.substr(0, 4) === "http") return url;
            var base = $('head > base').attr('href');
            return base + url;
        },
        login: function () {
            var $form = $('.form-signin');
            var $login = $('.login-wrap');

            //$('[data-bs-toggle="tooltip"]').tooltip();

            // handle input event
            $form.on('change', 'input', function () {
                // dispose login wrap error
                $login.tooltip('dispose');

                var $this = $(this);
                var val = $this.val();
                if (val.length > 0) {
                    $this.removeClass('is-invalid');
                    $this.tooltip('dispose');
                }
                else {
                    $this.addClass('is-invalid');
                    var handler = window.setTimeout(function () {
                        if (handler) {
                            window.clearTimeout(handler);
                        }
                        $this.tooltip('show');
                    }, 100);
                }
            });

            $('.btn-login').on('click', function (e) {
                e.preventDefault();
                var $userName = $form.find('[name="userName"]');
                var $password = $form.find('[name="password"]');
                var userName = $userName.val();
                var password = $password.val();

                if (userName === '') {
                    $userName.trigger("change");
                    return;
                }
                if (password === '') {
                    $password.trigger("change");
                    return;
                }

                var authenticate = function (postData) {
                    $.ajax({
                        url: $.formatUrl('api/login'),
                        data: postData,
                        method: 'POST',
                        contentType: 'application/json',
                        dataType: 'json',
                        crossDomain: false,
                        success: function (resp) {
                            //obj.invokeMethodAsync(method, userName, resp.Success);
                            if (!resp.success) {
                                console.log(resp.msg);
                                alert(resp.msg);
                                /*var $login = $('.login-wrap').addClass('is-invalid').tooltip({
                                    title: resp.Msg
                                });
                                var handler = window.setTimeout(function () {
                                    if (handler) {
                                        window.clearTimeout(handler);
                                    }
                                    $login.tooltip('show');
                                }, 100);*/
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            console.log(errorThrown);
                        }
                    });
                }

                //var postData = JSON.stringify({ userName, password });

                if (userName && password)
                    authenticate(JSON.stringify({
                        UserName: userName,
                        Password: password,
                        IsRemember: $('#IsRemember').val()
                    }));
                else
                    alert('请输入用户名、密码');
            });
        }
    });

    //$.login();
})(jQuery);
